package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.service.MajorService;
import com.zyd.shiro.business.vo.MajorConditionVO;
import com.zyd.shiro.persistence.beans.Major;
import com.zyd.shiro.persistence.mapper.MajorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MajorServiceImpl implements MajorService {

    @Autowired
    private MajorMapper majorMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public PageInfo<Major> findPageBreakByCondition(MajorConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<Major> majors = majorMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(majors)) {
            return null;
        }
        majors.forEach(m -> {
            if(!ObjectUtils.isEmpty(m.getEstablishmentTime())){
                m.setDate(sdf.format(m.getEstablishmentTime()));
            }
        });
        PageInfo bean = new PageInfo<Major>(majors);
        bean.setList(majors);
        return bean;
    }

    @Override
    public int insertMajor(Major major) throws Exception {
        Assert.notNull(major, "Major不可为空！");
        major.setCreateTime(new Date());
        major.setUpdateTime(new Date());
        if(!ObjectUtils.isEmpty(major.getDate())){
            try{
                major.setEstablishmentTime(sdf.parse(major.getDate()));
            }catch (Exception e){
                throw new Exception("日期格式不对");
            }
        }

        return majorMapper.insert(major);
    }

    @Override
    public boolean updateMajorSelective(Major entity) throws Exception {
        Assert.notNull(entity, "Major不可为空！");
        entity.setUpdateTime(new Date());
        if(!ObjectUtils.isEmpty(entity.getDate())){
            try{
                entity.setEstablishmentTime(sdf.parse(entity.getDate()));
            }catch (Exception e){
                throw new Exception("日期格式不对");
            }
        }
        return majorMapper.updateByPrimaryKeySelective(entity) > 0;
    }

    @Override
    public Major insert(Major entity) {
       return null;
    }

    @Override
    public void insertList(List<Major> entities) {

    }

    @Override
    public boolean removeByPrimaryKey(Long primaryKey) {
        return majorMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    public boolean update(Major entity) {
        return false;
    }

    @Override
    public boolean updateSelective(Major entity) {
        return true;
    }

    @Override
    public Major getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        Major major = majorMapper.selectByPrimaryKey(primaryKey);
        if(!ObjectUtils.isEmpty(major.getEstablishmentTime())){
            major.setDate(sdf.format(major.getEstablishmentTime()));
        }
        return null == major ? null : major;
    }

    @Override
    public Major getOneByEntity(Major entity) {
        return null;
    }

    @Override
    public List<Major> listAll() {
        return null;
    }

    @Override
    public List<Major> listByEntity(Major entity) {
        return null;
    }
}
